package com.steam.job;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.steam.entity.FromtaianBak;
import com.steam.entity.FromtaianBakExample;
import com.steam.entity.Phone;
import com.steam.mapper.FromtaianBakMapper;
import com.steam.mapper.ItemMapper;
import com.steam.mapper.LogisticsMapper;
import com.steam.service.JedisClient;
import com.steam.util.GsonBuilderUtil;
//@Component
public class DezhouSpringJob3 {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private ItemMapper itemMapper;
	private static SqlSessionFactory sqlSessionFactory;
//	private static String FILEPATH = "/usr/local/apache-tomcat-8.0.39/";
	Gson gson = GsonBuilderUtil.create();
	static {
		String resource = "mybatis/DezhouSqlMap3.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	// @Scheduled(cron = "0/5 * 4-23 * * ? ")
	@Scheduled(fixedDelay = 100)
	public void jining() {
		SqlSession sqlSession = null;
		// 数据库会话实例
		FromtaianBakExample example = new FromtaianBakExample();
		try {
			// 创建数据库会话实例sqlSession
			sqlSession = sqlSessionFactory.openSession();
			FromtaianBakMapper mapper = sqlSession.getMapper(FromtaianBakMapper.class);
			String jishu = jedisClient.get("ltaian");
			if (StringUtils.isNotBlank(jishu)) {
				example.createCriteria().andIdGreaterThan(Long.parseLong(jishu));
			} else {
				//查询库里最大值
				Integer selectMaxId = mapper.selectMaxId();
				if(selectMaxId != null){
					example.createCriteria().andIdGreaterThanOrEqualTo((Long.parseLong(selectMaxId+"")));
				}
			}
			List<FromtaianBak> selectByExample = mapper.selectByExample(example);
			if (selectByExample.size() == 0) {// 没有取出数据
				example.clear();
				example.createCriteria().andIdEqualTo(Long.parseLong(jishu));
				List<FromtaianBak> selectByPrimaryKey = mapper.selectByExample(example);
				if (selectByPrimaryKey != null && selectByPrimaryKey.size() > 0) {

				} else {
					example.clear();
					example.createCriteria().andIdEqualTo(1l);
					List<FromtaianBak> selectByPrimaryKey2 = mapper.selectByExample(example);
					if (selectByPrimaryKey2 != null && selectByPrimaryKey2.size() > 0) {
						if (Duration.between(selectByPrimaryKey2.get(0).getSj().toInstant(), Instant.now()).getSeconds() > 600) {
						} else {
							jedisClient.set("ltaian", "0");
						}
					} else {
						jedisClient.set("ltaian", "0");
					}
				}
			} else {
				for (FromtaianBak tblinfo : selectByExample) {
					//获取电话集合,德州网拿电话用
					List<Phone> phone_arr = new ArrayList<>();
					String o_phones = tblinfo.getDh();
					String[] phoneList = o_phones.split(" ");
					if(phoneList != null && phoneList.length>0){
	                    for (int i=0;i<phoneList.length;i++){
	                    	Phone phone = new Phone();
	                    	phone.setPhone(phoneList[i]);
	                    	phone_arr.add(phone);
	                    }
	                }
					com.steam.entity.Item items = new com.steam.entity.Item();
					items.setCreateTime(tblinfo.getSj());// 创建信息的时间
					items.setUserPhones(tblinfo.getDh());// 电话信息
					items.setContent(tblinfo.getNr());//信息的内容
					items.setCity(tblinfo.getShi());//出发地 市级单位
					items.setFormin("泰安");//数据的来源 来自于连云港的信息
					//推送给德州网平台bean
					Item item = new Item();
					item.setContent(tblinfo.getNr());
					//item.setPhones(sb.toString().trim());
					item.setTime(tblinfo.getSj());
					User user = new User();
					user.setPhoneList(phone_arr);
					item.setUser(user);
					item.setlTime(tblinfo.getSj().getTime());
					if(tblinfo.getLx().matches("货源")){
						items.setTypeId(1);
						item.setTypeId(1);
						try {
							itemMapper.insertSelective(items);
						} catch (Exception e) {
							e.printStackTrace();
							jedisClient.set("ltaian", tblinfo.getId().toString());
						}
						if(items.getCity() != null && !items.getCity().equals("德州")){
							rabbitTemplate.convertAndSend("*."+items.getCity(), gson.toJson(item));
						}
					}else{
						items.setTypeId(2);
						item.setTypeId(2);
						try {
							itemMapper.insertSelective(items);
						} catch (Exception e) {
							e.printStackTrace();
							jedisClient.set("ltaian", tblinfo.getId().toString());
						}
						if(items.getCity() != null && !items.getCity().equals("德州")){
							rabbitTemplate.convertAndSend("*."+items.getCity(), gson.toJson(item));
						}
					}
					jedisClient.set("ltaian", tblinfo.getId().toString());
				}
			}
		} catch (Exception e) {
			String jishu = jedisClient.get("ltaian");
			jedisClient.set("ltaian", Integer.parseInt(jishu) + 1 + "");
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}
}